﻿using System;
using System.Diagnostics;
using System.Threading;
using Wolfpack.Core.Interfaces.Entities;


namespace Wolfpack.Contrib.Checks.Drive
{
    public class SpaceCheckConfig : StreamThresholdCheckConfigBase
    {
        public string MachineName { get; set; }
        public string Drive { get; set; }
    }

    public class PerformanceCounterCheck : Wolfpack.Core.Checks.StreamThresholdCheckBase<SpaceCheckConfig>
    {
        protected PerformanceCounter myCounter;

        /// <summary>
        /// default ctor
        /// </summary>
        public PerformanceCounterCheck(SpaceCheckConfig config)
            : base(config)
        {
        }

        public override void Initialise()
        {
            myCounter = string.IsNullOrEmpty(myConfig.MachineName)
                ? new PerformanceCounter("LogicalDisk", "% Free Space", myConfig.Drive)
                : new PerformanceCounter("LogicalDisk", "% Free Space", myConfig.Drive, myConfig.MachineName);
        }

        public override void Execute()
        {
            var sample = myCounter.NextSample();
            Thread.Sleep(1000);
            var sample2 = myCounter.NextSample();
            var value = 100 - Math.Round(CounterSampleCalculator.ComputeCounterValue(sample, sample2));

            Publish(new HealthCheckData
            {
                Identity = Identity,
                Info = string.Format("Disk space used on drive '{0}' is {1}%", myConfig.Drive, value),
                Result = true,
                ResultCount = value,
                // allows you to find/filter on the specific drive when reporting
                Tags = myConfig.Drive
            });

        }

        protected override PluginDescriptor BuildIdentity()
        {
            return new PluginDescriptor
            {
                Description = "Reports the used disk space as %",
                TypeId = new Guid("F99F603F-B010-450D-8860-248340003E58"),
                Name = myConfig.FriendlyId
            };
        }
    }
}